package com.zuo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zuo.entity.Department;
import com.zuo.entity.Pos;
import com.zuo.entity.Staff;
import com.zuo.entity.User;
import com.zuo.service.DepartmentService;
import com.zuo.service.EmployeeService;
import com.zuo.service.PositionService;
import com.zuo.util.HrmConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 白白旧维
 * @Date 2022/3/3 10:13
 */
@Controller
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private PositionService positionService;

    @Autowired
    private DepartmentService departmentService;

    @RequestMapping("/employee")
    public String employee(Model model, @RequestParam(value = "page",defaultValue = "1") int page){
        PageHelper.startPage(page, HrmConstants.PAGE_DEFAULT_SIZE);

        List<Staff> staff = employeeService.listStaff();
        System.out.println("staff1 = " + staff);

        //PageInfo一定要获取到数据库集合之后
        PageInfo<Staff> pageInfo = new PageInfo<>(staff);
        int up = page-1;
        int down = page+1;
        int last = pageInfo.getLastPage();
        long total = pageInfo.getTotal();

        model.addAttribute("up",up);//上一页
        model.addAttribute("page",page);//当前页
        model.addAttribute("down",down);//下一页
        model.addAttribute("last",last);//最后一页
        model.addAttribute("total",total);//数据总数
        model.addAttribute("pageSize",HrmConstants.PAGE_DEFAULT_SIZE);//页面大小
        model.addAttribute("employees",staff);
        model.addAttribute("type","all");

        //下拉表部分
        List<Pos> pos = positionService.listPosition();
        List<Department> departments = departmentService.listDepartment();
        model.addAttribute("positions", pos);
        model.addAttribute("departments",departments);
        return "employee/employee.jsp";
    }

    @RequestMapping("/searchEmp")
    public String searchEmp(int pid,String sname,String scard,int sgender,String sphone,int did,Model model,
                            @RequestParam(value = "page",defaultValue = "1") int page){
        Map<String,Object> map = new HashMap<>();//多个参数使用Map集合
        map.put("pid",pid);
        map.put("sname",sname);
        map.put("scard",scard);
        map.put("sgender",sgender);
        map.put("sphone",sphone);
        map.put("did",did);

        PageHelper.startPage(page,HrmConstants.PAGE_DEFAULT_SIZE);

        List<Staff> staff = employeeService.searchStaff(map);

        //PageInfo一定要获取到数据库集合之后
        PageInfo<Staff> pageInfo = new PageInfo<>(staff);
        int up = page-1;
        int down = page+1;
        int last = pageInfo.getLastPage();
        long total = pageInfo.getTotal();

        model.addAttribute("up",up);//上一页
        model.addAttribute("page",page);//当前页
        model.addAttribute("down",down);//下一页
        model.addAttribute("last",last);//最后一页
        model.addAttribute("total",total);//数据总数
        model.addAttribute("pageSize",HrmConstants.PAGE_DEFAULT_SIZE);//页面大小
        model.addAttribute("employees",staff);
        model.addAttribute("type","search");
        model.addAttribute("pid",pid);
        model.addAttribute("sname",sname);
        model.addAttribute("scard",scard);
        model.addAttribute("sgender",sgender);
        model.addAttribute("sphone",sphone);
        model.addAttribute("did",did);

        //下拉表部分
        List<Pos> pos = positionService.listPosition();
        List<Department> departments = departmentService.listDepartment();
        model.addAttribute("positions", pos);
        model.addAttribute("departments",departments);
        return "employee/employee.jsp";
    }

    @RequestMapping("/toUpdateEmp")
    public String toUpdateEmp(int id,Model model){
        Staff staff = employeeService.getOne(id);
//        System.out.println("staff = " + staff);
        model.addAttribute("emp",staff);

        List<Pos> pos = positionService.listPosition();
        List<Department> departments = departmentService.listDepartment();
        model.addAttribute("positions", pos);
        model.addAttribute("departments",departments);
        return "employee/showUpdateEmployee.jsp";
    }

    @RequestMapping("/updateEmp")
    public String updateEmp(Staff staff,Model model){
//        System.out.println("staff = " + staff);
        int result = employeeService.updateStaff(staff);
        if(result!=0){
            model.addAttribute("info","成功修改了"+result+"条数据");
        }else{
            model.addAttribute("info","修改失败");
        }
        return "forward:employee.action";
    }

    @RequestMapping("/removeEmp")
    public String removeEmp(String ids,Model model){
        int result = employeeService.removeStaffs(ids);
        model.addAttribute("info","成功删除了"+result+"条记录");
        return "forward:employee.action";
    }

    @RequestMapping("/toAddEmp")
    public String toAddEmp(Model model){
        List<Pos> pos = positionService.listPosition();
        List<Department> departments = departmentService.listDepartment();
        model.addAttribute("positions", pos);
        model.addAttribute("departments",departments);
        return "employee/showAddEmployee.jsp";
    }

    @RequestMapping("/addEmp")
    public String addEmp(Staff staff,Model model){
        int result = employeeService.addStaff(staff);
        if(result!=0){
            model.addAttribute("info","添加成功");
        }else{
            model.addAttribute("info","添加失败");
        }
        return "forward:employee.action";
    }

}
